package com.gdcp.controller.portal;

import com.gdcp.beans.Collect;
import com.gdcp.beans.Product;
import com.gdcp.beans.Shop;
import com.gdcp.beans.User;
import com.gdcp.common.ResponseModel;
import com.gdcp.common.page.PageBean;
import com.gdcp.config.RequestHolder;
import com.gdcp.service.CollectService;
import com.gdcp.service.ShopService;
import com.gdcp.service.UserService;
import com.gdcp.util.GetPcUtil;
import com.gdcp.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/render/collection/")
public class CollectionController {

    @Autowired
    ResponseModel responseModel;
    @Autowired
    CollectService collectService;
    @Autowired
    ShopService shopService;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> insert(Integer shop_id, String shop_name){
        User user= (User) RequestHolder.getSession("user");
        //检查下是否已经收藏
        int checkCount=collectService.checkByShopIdAndUserId(shop_id, user.getId());
        if (checkCount>0){
            return responseModel.setModel(false,"收藏失败，你已经收藏过了。");
        }

        int count=collectService.insert(shop_id, shop_name, user.getId());
        if (count<=0){
            return responseModel.setModel(false,"收藏失败");
        }

        return responseModel.setModel(true,"收藏成功");
    }

    @RequestMapping(value = "/favorite")
    public String queryAllByUserId(){
        HttpServletRequest request=RequestHolder.getRequest();
        //当前页码
        int pc = GetPcUtil.getPc(request);
        User user= (User) RequestHolder.getSession("user");
        PageBean<Collect> pb = collectService.queryByPage(user.getId(), pc);
        List<Collect> collects=pb.getBeanList();

        for (Collect collect:collects){
            Shop shop=shopService.getInformation(collect.getShop_id());
            User user1=userService.getInformation(shop.getUser_id());
            if(user1!=null) {
                shop.setUser_image(user1.getImage());
                if(user1.getImage()!=null) {
                    collect.setShop_image(user1.getImage());
                }
            }
        }

        request.setAttribute("pb",pb);
        return "frontend/favorite";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Integer collect_id, Integer user_id){
        User user= (User) RequestHolder.getSession("user");
        if (user.getId().equals(user_id)){
            int count=collectService.delete(collect_id);
            if (count>0){
                return responseModel.setModel(true,"取消成功");
            }
        }

        return responseModel.setModel(false,"取消失败");
    }

}










    /*@RequestMapping(value = "/search",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> search(HttpServletRequest request){
        int pc = GetPcUtil.getPc(request);
        Integer user_id= (Integer) request.getAttribute("user_id");

        PageBean<Product> pb = collectService.queryByPage(user_id, pc);

        request.setAttribute("pb",pb);
        return responseModel.setModel(false,"取消失败");
    }*/